
## Define functions
## Simply to replicate plots
## Aggregate the data for each day of the RCS
## And then ggplot it

## For categorical variables : 
cat_ols <- function(data, moderator, ballot) {
  
  data[[moderator]] <- as.factor(data[[moderator]])
  
  dfagr <<- data %>%
    group_by(days) %>%
    count(days, .data[[moderator]]) %>%
    mutate(percentage = n / sum(n) * 100) 
  
  graf <<- ggplot(dfagr) +
    aes(x = days, y = percentage, color = .data[[moderator]], fill = .data[[moderator]]) +
    labs(title = ballot)
  }


## For continuous variables : 
num_ols <- function(data, numvar, ballot) {
  
  data[[numvar]] <- as.numeric(data[[numvar]])
  
  dfagr <<- data %>%
    select(all_of(numvar), days) %>% 
    arrange(days) %>% 
    group_by(days) %>% 
    summarise(avg = mean(.data[[numvar]], na.rm = T)) 
  
  graf <<- ggplot(dfagr) +
    aes(x = days, y = avg) +
    labs(title = ballot)
  }
